<?php

namespace App\Filament\Resources;

use App\Filament\Resources\ArticleResource\Pages;
use App\Models\Article;
use Filament\Forms\Components\Radio;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\Textarea;
use Filament\Forms\Components\MarkdownEditor;
use Filament\Forms\Form;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Actions\ActionGroup;
use Filament\Tables\Actions\DeleteAction;
use Filament\Tables\Actions\EditAction;
use Filament\Tables\Actions\ViewAction;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Columns\ToggleColumn;
use Filament\Tables\Table;

class ArticleResource extends Resource
{
    protected static ?string $model = Article::class;

    protected static ?string $navigationIcon = 'heroicon-o-document-duplicate';

    protected static ?int $navigationSort = 1;

    // 导航项分组翻译
    public static function getNavigationGroup(): ?string
    {
        return __('menu.article_group.name');
    }

    // 菜单标题导航翻译
    public static function getNavigationLabel(): string
    {
        return __('menu.article_group.article');
    }

    // 面包屑导航翻译
    public static function getModelLabel(): string
    {
        return __('menu.article_group.article');
    }

    public static function form(Form $form): Form
    {
        return $form
            ->schema([
                Select::make('category_id')
                    ->label(__('article.Category_name'))
                    ->relationship(name: 'category', titleAttribute: 'name')
                    ->preload()
                    ->searchable()
                    ->required(),
                TextInput::make('title')->label(__('article.Title'))->required(),
                Select::make('author_id')
                    ->label(__('article.Author_name'))
                    ->relationship(name: 'author', titleAttribute: 'name')
                    ->preload()
                    ->searchable()
                    ->required(),
                Textarea::make('description')
                    ->label(__('article.Description'))
                    ->rows(5)
                    ->maxLength(200)
                    ->required(),
                MarkdownEditor::make('content')->label(__('article.Content'))->required(),
                Radio::make('status')
                    ->label(__('music.Status'))
                    ->options([
                        '0' => __('music.Status_0'),
                        '1' => __('music.Status_1'),
                    ])
                    ->inline()
                    ->inlineLabel(false)
                    ->default('1')
                    ->required(),
            ])
            ->columns(1);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                TextColumn::make('id')
                    ->label(__('article.Id'))
                    ->sortable(),
                TextColumn::make('category.name')
                    ->label(__('article.Category_id'))
                    ->badge()
                    ->color('success')
                    ->alignCenter(),
                TextColumn::make('title')
                    ->label(__('article.Title'))
                    ->limit(50)
                    ->copyable()
                    ->searchable()
                    ->alignCenter(),
                TextColumn::make('author.name')
                    ->label(__('article.Author_name'))
                    ->badge()
                    ->color('gray')
                    ->alignCenter(),
                TextColumn::make('comment_quantity')->label(__('article.Comment_quantity'))
                    ->badge()
                    ->color('info')
                    ->alignCenter(),
                TextColumn::make('reading_quantity')->label(__('article.Reading_quantity'))
                    ->badge()
                    ->color('info')
                    ->alignCenter(),
                ToggleColumn::make('status')
                    ->label(__('article.Status'))
                    ->alignCenter(),
                TextColumn::make('created_at')
                    ->label(__('article.Created_at'))
                    ->dateTime('Y年m月d日 H时')
                    ->alignCenter(),
            ])
            ->filters([
                //
            ])
            ->actions([
                ActionGroup::make([
                    ViewAction::make(),
                    EditAction::make(),
                    DeleteAction::make(),
                ]),
            ])
            ->bulkActions([
                Tables\Actions\BulkActionGroup::make([
                    Tables\Actions\DeleteBulkAction::make(),
                ]),
            ])
            ->defaultSort('id', 'desc');
    }

    public static function getRelations(): array
    {
        return [
            //
        ];
    }

    public static function getPages(): array
    {
        return [
            'index' => Pages\ListArticles::route('/'),
        ];
    }
}
